package com.ywgt;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class StackCalculate {

    public static void main(String[] args) {
        //后缀表达式 1+（（2+3）*4）-5
        //后缀表达式 数字栈stack 1压入 ->2压入-> 3 压入->4压入->5压入
        //符号栈 +号压入 -> 左括号压入 -> 左括号压入 ->加号压入->右括号和左括号之间的符号
        // 压入数字栈 左右括号抵消 ->乘号遇括号压入->括号和左括号之间的符号 压入数字栈 左右括号抵消->
        // -号没有比+号优先 +号压入数字栈->-号压入数字栈
        //最后结果1 2 3 + 4 * + 5 -
        String mid="1+((2+3)*4)-5555";
        String suffix="7 6 * 1 1 4 + * + 5 -";
        List<String> list=getList(suffix);
        int calculate = calculate(list);
        System.out.println(calculate);
        List<String> convert = convert(mid);
        System.out.println(convert);
        String suffix1 = getSuffix(convert);
        List<String> list1 = getList(suffix1);
        int calculate1 = calculate(list1);
        System.out.println(calculate1);
    }

    /**
     *将集合转为后缀表达式
     */
    public static String getSuffix(List<String> list){
        Stack<String> nums=new Stack<>();
        Stack<String> symbol=new Stack<>();
        for (String s : list) {
            if(s.matches("[0-9][0-9]*")){
                nums.push(s);
            }else {

                String peek =null;
                if(!symbol.isEmpty()){
                    peek=symbol.peek();
                }
                if(peek==null||s.equals("(")){
                    symbol.push(s);
                    continue;
                }
                if(s.equals(")")){
                    while(!"(".equals(symbol.peek())){
                        nums.push(symbol.pop());
                    }
                    symbol.pop();
                    continue;
                }
                if(getInfo(s)<=getInfo(peek)&&!peek.equals("(")&&!peek.equals(")")){
                    nums.push(symbol.pop());
                    symbol.push(s);
                }else {
                    symbol.push(s);
                }
            }
        }
        String str="";
        while(!symbol.isEmpty()){
            nums.push(symbol.pop());
        }
        while(!nums.isEmpty()){
            str+=nums.pop()+" ";
        }
        StringBuffer buffer=new StringBuffer();
        buffer.append(str);
        StringBuffer reverse = buffer.reverse();
        String reveser = String.valueOf(reverse);
        String trim = reveser.trim();
        return trim;
    }

    public static int getInfo(String s1){
        int res=0;
        switch (s1) {
            case "+":
                res= 0;
            break;
            case "-":
                res= 0;
            break;
            case "*":
                res= 1;
            break;
            case "/":
                res= 1;
            break;
            default:res=2;
        }
        return res;
    }

    /**
     * 将中缀表达式的元素放入集合
     * @param mid
     * @return
     */
    public static List<String> convert(String mid){
        String[] split = mid.split("");
        List<String> ele=new ArrayList<>();
        for (int i = 0; i < split.length;) {
            String str="";
            while(split[i].matches("^[1-9]\\d*$")){
                str+=split[i];
                i++;
                if(i==split.length){
                    break;
                }
            }
            if(!"".equals(str))
            ele.add(str);
            if(i< split.length) {
                if (!split[i].matches("^[1-9]\\d*$")) {
                    ele.add(split[i]);
                    i++;
                    continue;
                }
            }
        }
        return ele;
    }

    /**
     * 将后缀表达式存放进list集合
     * @param suffix
     * @return
     */
    public static List<String> getList(String suffix){
        String[] s = suffix.split(" ");
        List<String> stringList=new ArrayList<>();
        for (String ele:s){
            stringList.add(ele);
        }
        return stringList;
    }

    public static int calculate(List<String> list){
        Stack<String> stack=new Stack<>();
        int res=0;
        for (String s : list) {
            if(s.matches("\\d+")){
                stack.push(s);
            }else {
                int num2 =Integer.parseInt(stack.pop());
                int num1=Integer.parseInt(stack.pop());
                switch (s){
                    case "+":
                        res=num1+num2;
                        break;
                    case "-":
                        res=num1-num2;
                        break;
                    case "*":
                        res=num1*num2;
                        break;
                    case "/":
                        res=num1/num2;
                        break;
                }
                stack.push(""+res);
            }
        }
        return  Integer.parseInt(stack.pop());
    }

}
